import javax.swing.AbstractAction;
import javax.swing.BorderFactory;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.KeyStroke;

import java.awt.Dimension;
import java.awt.HeadlessException;
import java.awt.Point;
import java.awt.Toolkit;

import javax.swing.JButton;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.io.File;
import java.sql.SQLException;

/* 
 *	NewForm.java - This class is used for adding new employee to the database.
 *
 *	@Author : BilCIS
 */


public class NewForm extends JFrame
{
	/**
	 * 
	 */
	private static final long serialVersionUID = 3430338376704955368L;

	// Variables declaration
	private JPanel contentPane;
	
	private JTextField txtName;
	private JTextField txtSurname;
	private JTextField txtBirthdate;
	private JTextField txtGender;
	private JTextField txtTitle;
	private JTextField txtCurrentSalary;
	private JTextField txtAddress;
	private JTextField txtNumberOfPermission;
	private JTextField emptyTxtName;
	private JTextField emptyTxtSurName;
	private JTextField emptyTxtBirthDate;
	private JTextField emptyTxtGender;
	private JTextField emptyTxtTitle;
	private JTextField emptyTxtSalary;
	private JTextField emptyTxtAddress;
	private JTextField emptyTxtNumber;
	
	private JButton btnAdd;
	private JButton btnCancel;
	
	private String name;
	private String surname;
	private String birthDate;
	private String gender;
	private String title;
	private String salary;
	private String address;
	private String number;	
	
	private	KeyStroke key = KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE,0);	

	private AbstractAction action;
	
	static Point mouseDownCompCoords = null;
	
	private Mysql mysql;
	private Employee emp;
	// End of variables declaration
	
	public NewForm( Employee emp)
	{
		this.emp = emp;
		create();
		mysql = new Mysql(emp.getID());
		this.setVisible(true);
	}

	private void create() 
	{
		//
		//Create controls
		//
		contentPane = new JPanel(null);
		txtName = new JTextField("Name");
		txtSurname = new JTextField("Surname");
		txtBirthdate = new JTextField("BirthDate");
		txtGender = new JTextField("Gender");
		txtTitle = new JTextField("Title");
		txtCurrentSalary = new JTextField("Current Salary");
		txtAddress = new JTextField("Address");
		txtNumberOfPermission = new JTextField("Cell Number");
		emptyTxtName = new JTextField();
		emptyTxtSurName = new JTextField();
		emptyTxtBirthDate = new JTextField();
		emptyTxtGender = new JTextField();
		emptyTxtTitle = new JTextField();
		emptyTxtSalary = new JTextField();
		emptyTxtAddress = new JTextField();
		emptyTxtNumber = new JTextField();
		btnAdd = new JButton("Add");
		btnCancel = new JButton("Cancel");
		//	
		//Register action listeners
		//
		btnAdd.addActionListener(new btnAdd_Action());
		btnCancel.addActionListener(new btnCancel_Action());
		//	
		// Mnemonics
		//
		btnAdd.setMnemonic(KeyEvent.VK_A);
		btnCancel.setMnemonic(KeyEvent.VK_C);
		//
		//Add controls to pane
		//
		this.add(contentPane);
		contentPane.add(txtName);
		contentPane.add(txtSurname);
		contentPane.add(txtBirthdate);
		contentPane.add(txtGender);
		contentPane.add(txtTitle);
		contentPane.add(txtCurrentSalary);
		contentPane.add(txtAddress);
		contentPane.add(txtNumberOfPermission);
		contentPane.add(emptyTxtName);
		contentPane.add(emptyTxtSurName);
		contentPane.add(emptyTxtBirthDate);
		contentPane.add(emptyTxtGender);
		contentPane.add(emptyTxtTitle);
		contentPane.add(emptyTxtSalary);
		contentPane.add(emptyTxtAddress);
		contentPane.add(emptyTxtNumber);
		contentPane.add(btnAdd);
		contentPane.add(btnCancel);
		//	
		//Set bounds
		//
		contentPane.setBounds(0, 0, 400, 350);
		txtName.setBounds(20, 28, 140, 20);
		txtSurname.setBounds(20, 59, 140, 20);
		txtBirthdate.setBounds(20, 91, 140, 20);
		txtGender.setBounds(20, 122, 140, 20);
		txtTitle.setBounds(20, 153, 140, 20);
		txtCurrentSalary.setBounds(20, 184, 140, 20);
		txtAddress.setBounds(20, 215, 140, 20);
		txtNumberOfPermission.setBounds(20, 246, 140, 20);
		emptyTxtName.setBounds(160, 28, 180, 21);
		emptyTxtSurName.setBounds(160, 59, 180, 21);
		emptyTxtBirthDate.setBounds(160, 91, 180, 21);
		emptyTxtGender.setBounds(160, 122, 180, 21);
		emptyTxtTitle.setBounds(160, 153, 180, 21);
		emptyTxtSalary.setBounds(160, 184, 180, 21);
		emptyTxtAddress.setBounds(160, 215, 180, 21);
		emptyTxtNumber.setBounds(160, 246, 180, 21);
		btnAdd.setBounds(100, 287, 100, 50);
		btnCancel.setBounds(250, 287, 100, 50);
		//	
		//Editable
		//
		txtName.setEditable(false);
		txtName.setColumns(10);
		txtSurname.setEditable(false);
		txtSurname.setColumns(10);
		txtBirthdate.setEditable(false);
		txtBirthdate.setColumns(10);
		txtGender.setEditable(false);
		txtGender.setColumns(10);
		txtTitle.setEditable(false);
		txtTitle.setColumns(10);	
		txtCurrentSalary.setEditable(false);
		txtCurrentSalary.setColumns(10);
		txtAddress.setEditable(false);
		txtAddress.setColumns(10);
		txtNumberOfPermission.setEditable(false);
		txtNumberOfPermission.setColumns(10);
		emptyTxtName.setColumns(10);
		emptyTxtSurName.setColumns(10);
		emptyTxtBirthDate.setColumns(10);
		emptyTxtGender.setColumns(10);
		emptyTxtTitle.setColumns(10);
		emptyTxtSalary.setColumns(10);
		emptyTxtAddress.setColumns(10);
		emptyTxtNumber.setColumns(10);
		//
		//	Set border
		//
		contentPane.setBorder(BorderFactory.createTitledBorder("New Form"));
		
		
		//********************** Colors *********************//
		contentPane.setBackground(emp.getColor());
		
		//
		//	exit KEY action
		//
		action = new AbstractAction(){
			/**
			 * 
			 */
			private static final long serialVersionUID = 1L;

			public void actionPerformed (ActionEvent e)
			{
				dispose();
			}
		};
		contentPane.setInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT, contentPane.getInputMap());
		contentPane.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(key, "pressed");
		contentPane.getActionMap().put("pressed", action);

		//
		// New Form
		//
		Dimension scrSize = Toolkit.getDefaultToolkit().getScreenSize();
		this.setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
		this.setLocation(scrSize.width/4 , scrSize.height/4 );
		this.setIconImage(Toolkit.getDefaultToolkit().getImage(new File("working.png").getAbsolutePath()));
		this.setUndecorated(true);
		this.setSize(new Dimension(400, 350));
		this.setLayout(null);
		this.setTitle("New Form");
		// mouse cursor with undecorated
		this.addMouseListener(new MouseListener()
		{
		    public void mouseReleased(MouseEvent e) 
		    {
		        mouseDownCompCoords = null;
		    }
		    public void mousePressed(MouseEvent e) 
		    {
		        mouseDownCompCoords = e.getPoint();
		    }
		    public void mouseExited(MouseEvent e) {}
		    public void mouseEntered(MouseEvent e) {}
		    public void mouseClicked(MouseEvent e) {}
		});
		this.addMouseMotionListener(new MouseMotionListener()
		{
		    public void mouseMoved(MouseEvent e) {}
		    public void mouseDragged(MouseEvent e) 
		    {
			    Point currCoords = e.getLocationOnScreen();
			    setLocation(currCoords.x - mouseDownCompCoords.x, currCoords.y - mouseDownCompCoords.y);
		    }
		});
		// end of mouse cursor
	}
	
	// cancels new form
	public class btnCancel_Action implements ActionListener
	{
		public void actionPerformed (ActionEvent e)
		{
			dispose();
		}
	}
	
	// adds new employee to database
	public class btnAdd_Action implements ActionListener
	{
		public void actionPerformed (ActionEvent e)
		{
			name = emptyTxtName.getText();
			surname = emptyTxtSurName.getText();
			birthDate = emptyTxtBirthDate.getText();
			gender = emptyTxtGender.getText();
			title = emptyTxtTitle.getText();
			salary = emptyTxtSalary.getText();
			address = emptyTxtAddress.getText();
			number = emptyTxtNumber.getText();
			
			if ( name.equals("") || surname.equals("") || birthDate.equals("") || gender.equals("") || title.equals("") 
							  || salary.equals("") || address.equals("") || number.equals("") )
				JOptionPane.showMessageDialog(null,"Some informations are empty!");
			else
			{
				try 
				{
					double slr = Double.parseDouble(salary);
				
					int newID = mysql.create(  name,  surname,  birthDate,  gender,  title,  slr,  address, number);
				
					JOptionPane.showMessageDialog(null,"Employee created successfully!\nHis username is " 
														+ (newID) +
														" and his temporary password is " + (newID) 
														+ ".\nWelcome to Company!");
				} catch (HeadlessException e1) {
					e1.printStackTrace();
				} catch (SQLException e1) {
					e1.printStackTrace();
				}
			}

		}
	}
	
}
